package com.ztgf.shopping.api.client.template;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.bo.template.TempletPlatformRelationBO;
import com.ztgf.shopping.models.dto.template.TempletInfoDTO;
import com.ztgf.shopping.models.dto.template.TempletPlatformRelationDTO;
import com.ztgf.shopping.models.service.template.ITempletPlatformRelationService;
import com.ztgf.shopping.models.vo.template.TemplateInfoDVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 店铺模板关系  前端控制器
 * </p>
 *
 * @author YUE
 * @since 2020-05-23
 */
@RestController
@RequestMapping("/api/templetPlatformRelation")
@Slf4j
@Api(value = "TempletPlatformRelationApiController",tags = "店铺模板关系块Api表接口")
public class TempletPlatformRelationApiController {
    @DubboReference
    private ITempletPlatformRelationService iTempletPlatformRelationService;

    /**
     *
     * @param id 模板id
     * @return
     */
    @ApiOperation(value = "修改平台活动页默认的加载页面",notes = "修改平台活动页默认的加载页面")
    @PostMapping("/updateActiveTemplateDefault")
    public Result updateActiveTemplateDefault(Long id){
        try {
            iTempletPlatformRelationService.updateActiveTemplateDefault(id);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new Result(CodeEnum.FAIL_100008);
        }
        return new Result();
    }

    /**
     *
     * @param shopId 店铺id
     * @param templateId 模板id
     * @param type 0首页 1活动页 2商品详情页banner
     * @return
     */
    @ApiOperation(value = "修改默认的模板",notes = "修改默认的模板")
    @PostMapping("/updateTemplateDefault")
    public Result updateTemplateDefault(Long shopId,Long templateId,int type){
        try {
            iTempletPlatformRelationService.updateTemplateDefault(shopId,templateId,type);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new Result(CodeEnum.FAIL_100008);
        }
        return new Result();
    }

    /**
     *
     * @param shopId 店铺id
     * @param type 0首页 1活动页 2banner详情页
     * @return
     */
    @ApiOperation(value = "根据店铺id获取模板集合",notes = "根据店铺id获取模板集合")
    @PostMapping("/getAllTemplateByShopId")
    public Result<List<TemplateInfoDVO>> getAllTemplateByShopId(Long shopId, int type){
        List<TemplateInfoDVO> templetInfoDTOList = iTempletPlatformRelationService.getTempletInfoListByShopId(shopId,type);
        return new Result<>(templetInfoDTOList);
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<TempletPlatformRelationDTO> get(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        TempletPlatformRelationDTO templetPlatformRelationDTO = iTempletPlatformRelationService.selectById(id);
        return new Result<>(templetPlatformRelationDTO);
    }

    @ApiOperation(value = "店铺模板关系块表列表",notes = "店铺模板关系块表列表")
    @PostMapping("/queryPageList")
    public Result<Page<TempletPlatformRelationDTO>> queryPageList(TempletPlatformRelationBO templetPlatformRelationBO){
        Wrapper<TempletPlatformRelationDTO> wrapper = new EntityWrapper<>();
        Page<TempletPlatformRelationDTO> page = new Page<>(templetPlatformRelationBO.getPageNo(), templetPlatformRelationBO.getPageSize());
        page = iTempletPlatformRelationService.selectPage(page, wrapper);
        return new Result<>(page);
    }
}

